package com.avic.rong.userOrder.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.userOrder.adapter.MineOrderListAdapter;
import com.avic.rong.userOrder.api.OrderListApi;
import com.avic.rong.userOrder.bean.AllOrderListBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.EditUtils;
import com.avic.rong.utils.ToastTools;
import com.avic.rong.wedgit.loadingLayout.LoadingLayout;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class OrderSearchActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.et_search_title)
    EditText et_search_title;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.order_search_list)
    RecyclerView orderSearchList;
    @BindView(R.id.loading_layout)
    LoadingLayout loading_layout;



    private String order_type;
    private List<AllOrderListBean.OrderListBean> listData=new ArrayList<>();
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private MineOrderListAdapter mineOrderListAdapter;
    @Override
    protected int getContentViewId() {
        return R.layout.activity_order_search;
    }

    @Override
    protected void initView() {
        loading_layout.setStatus(LoadingLayout.SUCCESS);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);


        orderSearchList.setLayoutManager(new LinearLayoutManager(this));
       /* mineOrderSearchListAdapter = new MineOrderSearchListAdapter(context,listData);
        orderSearchList.setAdapter(mineOrderSearchListAdapter);*/
        mineOrderListAdapter = new MineOrderListAdapter(context,listData);
        orderSearchList.setAdapter(mineOrderListAdapter);

    }

    private void searchData() {
        if (!getSharedPreferencesUtil().getLogintoken().isEmpty()) {
            String searchTitle = et_search_title.getText().toString().trim();
            Map<String, String> map = new HashMap<>();
            if (TextUtils.isEmpty(searchTitle)){
                ToastTools.showToast(this,"请输入订单号/商品名称");
                return;
            }
            map.put("orderSn",searchTitle);
            map.put("orderType", "0");
            map.put("type", "0");
            map.put("pageNo",currentPageNumber+"");
            map.put("pageSize", "5");
            map.put("token", getSharedPreferencesUtil().getLogintoken());
            //配送方式列表
            final OrderListApi orderListApi = new OrderListApi(this, simpleOnNextListener);
            orderListApi.setCache(false);
            orderListApi.setMap(map);
            HttpManager.getInstance().doHttpDeal(orderListApi);

            loading_layout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    HttpManager.getInstance().doHttpDeal(orderListApi);
                    loading_layout.setStatus(LoadingLayout.LOADING);
                }

                @Override
                public void onReturn(View v) {
                    onBackPressed();
                }
            });
//            loading_layout.setStatus(LoadingLayout.LOADING);
        } else {
            loading_layout.setStatus(LoadingLayout.EMPTY);
        }
    }

    @Override
    protected void addListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditUtils.hideSoftKeyboard(OrderSearchActivity.this,et_search_title);
                isRefresh = true;
                currentPageNumber = 1;
                searchData();

            }
        });
    }

    //权威评测
    HttpOnNextListener simpleOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (isRefresh){
                listData.clear();
            }
            if (!subjects.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)) {
                ToastTools.showToast(OrderSearchActivity.this, subjects.getMsg());
                return;
            }
            if (isRefresh){
                refreshLayout.finishRefresh();
            }else{
                refreshLayout.finishLoadMore();
            }
            loading_layout.setStatus(LoadingLayout.SUCCESS);
            AllOrderListBean allOrderListBean = (AllOrderListBean) subjects.getData();
            List<AllOrderListBean.OrderListBean> orderList = allOrderListBean.getOrderList();

            mineOrderListAdapter.setBiaoZhunImg(allOrderListBean.getBiaoZhunImg());;
            mineOrderListAdapter.setFileServer(allOrderListBean.getFileServer());
            listData.addAll(orderList);
            if (listData!=null&&listData.size()<=0){
                loading_layout.setStatus(LoadingLayout.EMPTY);
            }
            mineOrderListAdapter.notifyDataSetChanged();
        }


        @Override
        public void onCacheNext(String cache) {


        }


        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", e.getMessage());
            if (isRefresh){
                refreshLayout.finishRefresh();
            }else{
                refreshLayout.finishLoadMore();
            }
            if (e instanceof SocketTimeoutException || e instanceof ConnectException) {
                loading_layout.setStatus(LoadingLayout.NO_NETWORK);
            } else {
                loading_layout.setStatus(LoadingLayout.ERROR);
            }
        }
    };

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isRefresh = false;
        currentPageNumber = currentPageNumber + 1;
        searchData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isRefresh = true;
        currentPageNumber = 1;
        searchData();
    }
}
